import os
import yaml
import base64
import hashlib
import json


def root_path():
    """ 获取 根路径 """
    path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
    return path


def ensure_path_sep(path):
    """兼容 windows 和 linux 不同环境的操作系统路径 """
    if "/" in path:
        path = os.sep.join(path.split("/"))

    if "\\" in path:
        path = os.sep.join(path.split("\\"))

    return root_path() + path


def load_yaml(file_path):
    """读取yaml并return"""
    with open(file_path, mode='r', encoding='utf-8') as file:
        data = yaml.load(file, Loader=yaml.FullLoader)
    return data


def load(file_name, path):
    """在项目内读取指定文件"""
    ls = os.listdir(path)
    for i in ls:
        c_path = os.path.join(path, i)  # 将目录和文件名拼接
        if os.path.isdir(c_path):  # 如果是目录继续调用
            data = load(file_name, c_path)
            if data is not None:
                return data
        elif file_name == i:
            data = load_yaml(c_path)
            return data

def md5(data):
    """md5加密处理"""
    data = data + 'test'
    md_5 = hashlib.md5()
    md_5.update(data.encode('utf-8'))
    return md_5.hexdigest()


def enconde_bs64(data):
    data = json.dumps(data, indent=4, ensure_ascii=True)
    encode64 = base64.b64encode(data.encode('utf-8'))
    return encode64.decode('utf-8')


def decode64_str(data):
    decode64 = base64.b64decode(data)
    decode64_str = decode64.decode('utf-8')
    return decode64_str


if __name__ == '__main__':
    s = 'hello word'
    t = enconde_bs64(s)
    print(t)
    # c = decode64_str(t)
    # a = json.loads(c)
    # print(c, a)
    # data = load_yaml(rootDir + r'\data_object\retonefit_api\temp.yaml')
    # print(data)

    d1 = {"nodata": "nodata"}
    data = d1.get("nodata")
    print(data)
